<?php
/**
用户管理
 */
namespace app\tdadmin\controller;
use app\common\controller\AdminBaseController;
use think\Db;
use \tp5er\Backup;
use think\facade\Config;
use \org\Baksql;
class DatabaseController extends AdminBaseController
{
    public  $config=array(
        'path' => './Data/',//数据库备份路径
        'part' => 20971520,//数据库备份卷大小
        'compress' => 0,//数据库备份文件是否启用压缩 0不压缩 1 压缩
        'level' => 9 //数据库备份文件压缩级别 1普通 4 一般 9最高
    );
	public function index()
	{
		return $this->fetch();

	}

	public function getList(){

        $db= new Backup($this->config);

        $tables=$db->dataList();//获取数据库所有表的信息
//        foreach($tables as $k=>$v){
//            $db->backup($v['name'],0);//循环所有表备份表和数据
//        }
//        $file=$db->getFile();//获取所备份文件的文件名

        return [
            'code'=>0,
            'msg'=>'成功',
            'data'=>$tables
        ];

    }

    public function backup(){

        $config = Config::get();
//        $sql=new Baksql($config['database']);
        $db= new Backup($this->config);
        $tables = $this->request->param("name");

        for ($i = 0; $i < count($tables); $i++){
            $info = $db->backup($tables[0],0);
        }
        addLog("备份了数据库","");

        $this->success("$info",'database/index');
    }

    public function optimize(){
        $db= new Backup($this->config);

        $name = $this->request->param("name");
        if ($name){
            try {
               $db->optimize([$name]);
                addLog("优化了数据库","");

                return [
                    'code' => 0,
                    'msg' => '优化完成'
                ];
            } catch (\Exception $e) {
                return [
                    'code' => 1,
                    'msg' => '优化失败'
                ];
            }
        }else{
            return [
                'code' => 1,
                'msg' => '请选择要优化的表'
            ];
        }
    }
    public function repair(){
        $db= new Backup($this->config);

        $name = $this->request->param("name");
        if ($name){
            try {
               $db->repair([$name]);
                addLog("修复了数据库","");

                return [
                    'code' => 0,
                    'msg' => '修复完成'
                ];
            } catch (\Exception $e) {
                return [
                    'code' => 1,
                    'msg' => '修复失败'
                ];
            }
        }else{
            return [
                'code' => 1,
                'msg' => '请选择要修复的表'
            ];
        }
    }

    public function filelist(){
        return $this->fetch();
    }

    public function getFileList(){
        $db= new Backup($this->config);
        $list = $db->fileList();
        return [
            'code'=>0,
            'data' => $list
        ];
    }


    public function download(){
	    $time = $this->request->param("time");
        $db= new Backup($this->config);
        try {
            $list = $db->downloadFile($time, 0);
            addLog("下载了数据库文件","");

        } catch (\Exception $e) {
        }
        return [
            'code'=>0,
            'data' => $list
        ];
    }
    public function restore(){
	    $time = $this->request->param("time");

        $db= new Backup($this->config);
        try {
            $file = $db->getFile('timeverif', $time);
            addLog("恢复了数据库文件","");

        } catch (\Exception $e) {
            return json([
                'code'=>1,
                'msg' => '恢复失败'
            ]);
        }
        $start=0;
        $start= $db->setFile( $file)->import($start);
        return json([
            'code'=>0,
            'msg' => '恢复成功'
        ]);
    }


    public function delete(){
	    $time = $this->request->param("time");

        $db= new Backup($this->config);
        try {
            $db->delFile($time);
            addLog("删除了数据库备份","");

        } catch (\Exception $e) {
            return json([
                'code'=>1,
                'msg' => '删除失败'
            ]);
        }
        return json([
            'code'=>0,
            'msg' => '删除成功'
        ]);


    }
}